<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8">
    <link rel="icon" href="./favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GoWebSSH</title>
    <style>
      html,
      body,
      #app {
        height: 100%;
        margin: 0px;
        padding: 0px;
        font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
      }
    </style>
    <script type="module" crossorigin src="./assets/index-D2Du4RXM.js"></script>
    <link rel="stylesheet" crossorigin href="./assets/index-1NBLLXCn.css">
  </head>

  <body>
    <noscript>
      <strong>请开启JavaScript支持</strong>
    </noscript>
    <div id="app"></div>
  </body>

  <script>
    document.addEventListener('contextmenu', (event) => {
      event.preventDefault(); // 禁用默认右键菜单

      const selection = window.getSelection();
      const clickedText = event.target.textContent;

      // 如果用户没有提前选择文本，则自动选择鼠标下的单词
      if (selection.toString().length === 0 && clickedText) {
        if (document.caretRangeFromPoint) {
          const range = document.caretRangeFromPoint(event.clientX, event.clientY);
          if (range) {
            expandRangeToWord(range);
            selection.removeAllRanges();
            selection.addRange(range);
          }
        }
      }

      // 复制选中的文本
      const selectedText = selection.toString().trim();
      if (selectedText) {
        copyToClipboard(selectedText);
        showNotification('复制成功: ' + selectedText);
      } else {
        showNotification('未选中任何文本');
      }
    });

    // 扩展选择范围到整个单词
    function expandRangeToWord(range) {
      // 向左扩展
      while (range.startOffset > 0) {
        const prevChar = range.startContainer.data[range.startOffset - 1];
        if (isWordChar(prevChar)) {
          range.setStart(range.startContainer, range.startOffset - 1);
        } else {
          break;
        }
      }

      // 向右扩展
      while (range.endOffset < range.endContainer.data.length) {
        const nextChar = range.endContainer.data[range.endOffset];
        if (isWordChar(nextChar)) {
          range.setEnd(range.endContainer, range.endOffset + 1);
        } else {
          break;
        }
      }
    }

    // 检查字符是否是单词的一部分（字母、数字、下划线）
    function isWordChar(char) {
      return /[\w]/.test(char);
    }

    // 复制文本到剪贴板
    function copyToClipboard(text) {
      const textArea = document.createElement('textarea');
      textArea.value = text;
      textArea.style.position = 'fixed';
      textArea.style.top = 0;
      textArea.style.left = 0;
      document.body.appendChild(textArea);
      textArea.focus();
      textArea.select();

      try {
        document.execCommand('copy');
      } catch (err) {
        console.error('复制失败:', err);
      }

      document.body.removeChild(textArea);
    }

    // 显示通知
    function showNotification(message) {
      const notification = document.getElementById('notification');
      notification.textContent = message;
      notification.classList.add('show');

      setTimeout(() => {
        notification.classList.remove('show');
      }, 2000);
    }
  </script>
</html>
